package com.ruge._08_springboot_springdata_jpa.controller._05_query;

import com.ruge._08_springboot_springdata_jpa.Application;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * 创建人 ：爱丽丝、如歌
 * 创建时间 ：2019-07-05  22:44
 * 描述 ：
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)//这里是启动类
public class TestQueryEmployee {
    @Autowired
    private EntityManager entityManager;

    @Before
    @Transactional
    public void testInitData() {
        Query nativeQuery = entityManager.createNativeQuery("delete from t_query_employee");
        nativeQuery.executeUpdate();
        for (int i = 0; i < 10; i++) {
            QueryEmployeeModel model = new QueryEmployeeModel("张" + i, i, i);
            entityManager.merge(model);
            entityManager.flush();
        }
    }

    /**
     * 查询所有员工信息
     */
    @Test
    @Transactional
    public void testAll() {
        List list = entityManager.createQuery("select t from QueryEmployeeModel t").getResultList();
        System.out.println("=========查询成功=========");
        list.forEach(System.out::println);
    }

    /**
     * 查询所有员工信息
     */
    @Test
    @Transactional
    public void testCondition() {
        Query query = entityManager.createQuery("select t from QueryEmployeeModel t where t.age>?1 and t.name like :name");
        query.setParameter(1, 4);
        query.setParameter("name", "%8%");
        List<QueryEmployeeModel> list = query.getResultList();
        System.out.println("=========查询成功========="+list.get(0).getName());
        list.forEach(System.out::println);
    }

    @Test
    @Transactional
    public void testPage() {
        Query query = entityManager.createQuery("select t from QueryEmployeeModel t order by id desc");
        for (int i = 0; i <10 ; i++) {
        query.setFirstResult(i*5).setMaxResults(5);
        List<QueryEmployeeModel> list = query.getResultList();
        System.out.println("=========查询成功========="+i+"===页");
        list.forEach(System.out::println);
        }
    }

    @Test
    @Transactional
    public void testNameQuery() {
        Query query = entityManager.createNamedQuery("selectAll");
        List<QueryEmployeeModel> list = query.getResultList();
        System.out.println("=========查询成功=========");
        list.forEach(System.out::println);
    }
    @Test
    @Transactional
    public void testNativeQuery() {
        Query query = entityManager.createNativeQuery("select  * from t_query_employee",QueryEmployeeModel.class);
        List list = query.getResultList();
        System.out.println("=========查询成功=========");
        list.forEach(System.out::println);
    }
}
